<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'
import { fetchProjectList } from '@/api/project';

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart chart-container'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '500px'
    }
  },
  data() {
    return {
      chart: null,
      totalList: []
    }
  },
  created() {
    this.getList();
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    getList() {
      fetchProjectList().then((res) => {
        let list1=0;
        let list2=0;
        let list3=0;
        let list4=0;
        
        res.data.forEach(project => {
          if (project.projectStatus === '项目未立项') {
            list1 += 1;
          } else if (project.projectStatus === '项目进行中') {
            list2 += 1;
          } else if (project.projectStatus === '项目已结项') {
            list3 += 1;
          } else {
            list4 += 1;
          }
        });
        this.totalList.push({value: list1, name: '未立项'});
        this.totalList.push({value: list2, name: '进行中'});
        this.totalList.push({value: list3, name: '已结项'});
        this.totalList.push({value: list4, name: '其它'});

        this.initChart();
      });
    },
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')

      this.chart.setOption({
        tooltip: {
          trigger: 'item',
          formatter: '{a} <br/>{b} : {c} ({d}%)'
        },
        legend: {
          left: 'center',
          bottom: '10',
          data: ['未立项', '进行中', '已结项', '其它']
        },
        series: [
          {
            name: '项目数量',
            type: 'pie',
            roseType: 'radius',
            radius: [15, 95],
            center: ['50%', '38%'],
            data: this.totalList,
            animationEasing: 'cubicInOut',
            animationDuration: 2600
          }
        ]
      })
    }
  }
}
</script>
<style>
.chart-container {
  padding: 1rem;
}
</style>
